package com.tanhua.dubbo.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mapper.*;
import com.tanhua.model.db.*;
import com.tanhua.model.vo.OptionVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@DubboService
public class QuestionnaireApiImpl implements QuestionnaireApi {
    @Autowired   //用户与问卷表
    private UserQuestionnaireMapper userQuestionnaireMapper;

    @Autowired //单纯的问卷表
    private QuestionnaireMapper questionnaireMapper;

    @Autowired  //问卷题目
    private QQuestionMapper qQuestionMapper;

    @Autowired   //题目的选项
    private OptionMapper optionMapper;

    @Autowired //报告相关
    private ReportMapper reportMapper;

    @Autowired //维度相关
    private ExtraMapper extraMapper;

    @Autowired
    private SimilarMapper similarMapper;


    //查询与用户相关的问卷表是否存在
    public Map<String, UserQuestionnaire> findUQ(long userId) {
        //通过useId查询
        QueryWrapper<UserQuestionnaire> qw = new QueryWrapper<>();
        qw.eq("userId", userId);
        List<UserQuestionnaire> userQuestionnaires = userQuestionnaireMapper.selectList(qw);
        Map<String, UserQuestionnaire> map = new HashMap<>();
        //判断表数据是否存在
        if (userQuestionnaires != null && userQuestionnaires.size() > 0) {
            for (UserQuestionnaire userQuestionnaire : userQuestionnaires) {
                map.put(userQuestionnaire.getQuestionnaireId(), userQuestionnaire);
            }
            //存在返回map
            return map;
        }
        //不存在返回null
        return null;
    }

    //查询问卷
    public List<Questionnaire> findQuestionnaire() {
        List<Questionnaire> questionnaires = questionnaireMapper.selectList(null);
        return questionnaires;
    }

    //添加用户与问卷关系
    public void saveUQ(UserQuestionnaire uq) {
        userQuestionnaireMapper.insert(uq);
    }

    //问卷题目
    public List<QQuestion> findQuestion(String level) {
        List<QQuestion> question = qQuestionMapper.findQuestion(level);
        return question;
    }

    //查询题目对应的选项
    public List<OptionVo> findOption(Integer id) {
        QueryWrapper<Option> qw = new QueryWrapper<>();
        qw.eq("questionId", id);
        List<Option> options = optionMapper.selectList(qw);
        List<OptionVo> optionVos = new ArrayList<>();
        for (Option option : options) {
            OptionVo vo = new OptionVo();
            vo.setOption(option.getContent());
            vo.setId(option.getId().toString());
            optionVos.add(vo);
        }
        return optionVos;
    }

    //更新问卷状态
    public void updateUQ(UserQuestionnaire uq) {
        userQuestionnaireMapper.updateById(uq);
    }

    //添加报告
    public String saveReport(Report report, Set<Extra> list) {

        //添加报告
        System.out.println(list);
        reportMapper.insert(report);

        //添加维度信息
        for (Extra dimension : list) {
            dimension.setReportId(report.getId());
            dimension.setReportId(report.getId());
            System.out.println(dimension);
            extraMapper.insert(dimension);
        }
        return report.getId().toString();
    }

    //查询所有的报告信息  所有报告的
    public List<Report> findReportAll(String level) {
        //查询所有报告
        QueryWrapper<Report> qw1 = new QueryWrapper<>();
        qw1.eq("level", level);
        List<Report> reports = reportMapper.selectList(qw1);
        for (Report report : reports) {
            //查询相对应的维度信息
            List<Extra> extras = extraMapper.findByReportIdSortKeyOne(report.getId());
            report.setDimensions(extras.toArray());
        }
        return reports;
    }

    //通过userId查询报告   相似度自己的
    public Report findByUserIdReport(Long userId, String level) {
        Report report = reportMapper.findByLastReport(level, userId);
        List<Extra> extras = extraMapper.findByReportIdSortKeyOne(report.getId());
        report.setDimensions(extras.toArray());
        return report;
    }

    //添加相似度
    public void saveSimilar(Similar similar) {
        //添加自己的
        similarMapper.insert(similar);
        //添加与之匹配的用户的
        Long userId = similar.getUserId();
        Long sUserId = similar.getSUserId();
        similar.setSUserId(userId);
        similar.setUserId(sUserId);
        similarMapper.insert(similar);
    }

    //查询单个问题
    public QQuestion findQuestionById(String questionId) {
        return qQuestionMapper.selectById(questionId);
    }

    //查询报告是否存在
    public Report exitRepeatReport(long userId, String level) {
        QueryWrapper<Report> qw = new QueryWrapper<>();
        qw.eq("userId", userId);
        qw.eq("level", level);
        Report report = reportMapper.selectOne(qw);
        //判断是否存在
        if (report == null) {
            return null;
        }
        return report;
    }

    //解除问题表级别
    public void relieve(long userId, String level) {
        //查询问卷表的id
        QueryWrapper<Questionnaire> qw1 = new QueryWrapper<>();
        qw1.eq("level", level);
        Questionnaire questionnaire = questionnaireMapper.selectOne(qw1);
        //构造用户和问卷表的交叉表
        UserQuestionnaire uq = new UserQuestionnaire();
        uq.setIsLock(0);
        //更新用户与问卷表交叉的锁定状态
        QueryWrapper<UserQuestionnaire> qw2 = new QueryWrapper<>();
        qw2.eq("userId", userId);
        qw2.eq("questionnaireId", questionnaire.getId());
        userQuestionnaireMapper.update(uq, qw2);

    }

    //将报告id写入对应的数据库中
    public void saveUQReportId(String reportId, String level, long userId) {
        //获取问卷id
        QueryWrapper<Questionnaire> qw1 = new QueryWrapper<>();
        qw1.eq("level", level);
        Questionnaire questionnaire = questionnaireMapper.selectOne(qw1);
        //通过userId和问卷id查询用户与报告的表,
        QueryWrapper<UserQuestionnaire> qw2 = new QueryWrapper<>();
        qw2.eq("userId", userId);
        qw2.eq("questionnaireId", questionnaire.getId());
        //将报告设置进数据库
        UserQuestionnaire uq = new UserQuestionnaire();
        uq.setReportId(reportId);
        uq.setIsLock(0);
        userQuestionnaireMapper.update(uq, qw2);

    }

    //删除与自身相关的推荐表
    public void deleteSimilar(Long userId, String level) {//不需要level字段,数据库中没有
        QueryWrapper<Similar> qw = new QueryWrapper<>();
        qw.eq("userId", userId);
        qw.eq("level", level);
        System.out.println("删除成功");
        similarMapper.delete(qw);
        QueryWrapper<Similar> qw2 = new QueryWrapper<>();
        qw2.eq("sUserId", userId);
        qw2.eq("level", level);
        similarMapper.delete(qw2);

    }

    //删除报告表
    public void deleteReport(long userId, String level) {
        QueryWrapper<Report> qw = new QueryWrapper<>();
        qw.eq("userId", userId);
        qw.eq("level", level);
        reportMapper.delete(qw);
        System.out.println("删除成功");

    }

    //通过id查询报告
    public Report findByReportIdReprot(Integer reportId) {
        Report report = reportMapper.selectById(reportId);
        return report;
    }

    //通过报告id查询纬度值
    public List<Extra> findByReportIdExtra(Integer reportId) {
        QueryWrapper<Extra> qw = new QueryWrapper<>();
        qw.eq("reportId", reportId);
        return extraMapper.selectList(qw);
    }

    //查询相似的人
    public Similar findByUserIdSimilar(long userId) {
        List<Similar> similars = similarMapper.sortScore(userId);
        if (similars != null && similars.size() > 0) {
            return similars.get(0);
        }
        return null;
    }

    //删除以前报告的维度值
    public void deleteExtra(Integer id) {
        QueryWrapper<Extra> qw = new QueryWrapper<>();
        qw.eq("reportId", id);
        extraMapper.delete(qw);
    }


    /*
           自建后台
     */

    // 分页查询
    @Override
    public Page<Questionnaire> selectAll(Integer pageNumber, Integer pageSize) {

        // 指定分页参数
        Page<Questionnaire> pageParam  = new Page<>(pageNumber,pageSize);

        // 查询
        Page<Questionnaire> iPage = questionnaireMapper.selectPage(pageParam, null);

        return iPage;
    }

    @Override
    public Page<QQuestion> selectQuestions(Integer pageNumber, Integer pageSize, String level) {

        // 指定分页参数
        Page<QQuestion> pageParam  = new Page<>(pageNumber,pageSize);

        // 指定查询条件
        QueryWrapper<QQuestion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level",level);

        // 查询
        Page<QQuestion> iPage = qQuestionMapper.selectPage(pageParam, queryWrapper);

        return iPage;
    }

    // 保存问题
    @Override
    public Integer saveQuestions(QQuestion questions) {

        qQuestionMapper.insert(questions);

        Integer id = questions.getId();

        return id;
    }

    // 删除问题
    @Override
    public void deleteQuestion(Integer questionId) {

        qQuestionMapper.deleteById(questionId);
    }

    // 根据问题id查询问题
    @Override
    public QQuestion selectQuestionById(Integer questionId) {

        QQuestion question = qQuestionMapper.selectById(questionId);
        return question;
    }

    // 修改问题
    @Override
    public void updateQuestions(QQuestion questions) {

        qQuestionMapper.updateById(questions);
    }










}
